package com.cec.home.interceptor

import com.cec.home.service.impl.DynamicServiceImpl
import com.cec.home.service.impl.LogoServiceImpl
import com.cec.home.service.impl.MainNavServiceImpl
import com.cec.home.service.impl.PeopleServiceImpl
import com.cec.home.utils.PKUtils
import com.mongodb.BasicDBObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.ui.ModelMap
import org.springframework.web.context.request.WebRequest
import org.springframework.web.context.request.WebRequestInterceptor
/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 16/1/13
 */
class HeaderInterceptor implements WebRequestInterceptor {

    @Autowired
    MainNavServiceImpl mainNavService

    @Autowired
    LogoServiceImpl logoService

    @Autowired
    DynamicServiceImpl dynamicService

    @Autowired
    PeopleServiceImpl peopleService


    @Override
    void preHandle(WebRequest request) throws Exception {

    }

    @Override
    void postHandle(WebRequest request, ModelMap model) throws Exception {
        if (model == null) {
            return
        }
        //导航条
        def navs = this.mainNavService.getAll([status: 1] as BasicDBObject, [sort: 1] as BasicDBObject, null)
        model.addAttribute("navs", navs)

        //logo展示

        def lists = logoService.getAll([status: 1] as BasicDBObject, null, null)

        def logos = new ArrayList()

        lists.each {
            it.img = PKUtils.buildImg(it.img as String)
            logos.add(it)
        }

        model.addAttribute("logos", logos)

        //最新动态

        def dynamic = dynamicService.getAll([status: 1, type: 1] as BasicDBObject, null, null)

        def dynamics = new ArrayList()

        dynamic.each {
            it.img = PKUtils.buildImg(it.img as String)
            dynamics.add(it)
        }

        model.addAttribute("dynamics", dynamics)

        //联系人展示

        def people = peopleService.getAll(null, null, null)

        model.addAttribute("people", people)

    }

    @Override
    void afterCompletion(WebRequest request, Exception ex) throws Exception {

    }
}
